package com.pansoft.openplanet.widget;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import androidx.appcompat.app.AlertDialog;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.efounder.chat.manager.PictureAndCropManager;
import com.efounder.utils.EasyPermissionUtils;
import com.efounder.utils.ResStringUtil;
import com.pansoft.openplanet.R;

/**
 * 拍照或者选择头像的 diaolog
 *
 * @author yqs
 */

public class SelectAvatarDialog {

    private PictureAndCropManager pictureAndCropManager;
    private Context mContext;


    public SelectAvatarDialog(Context context, PictureAndCropManager pictureAndCropManager) {
        this.pictureAndCropManager = pictureAndCropManager;
        this.mContext = context;
    }

    public void show() {
        final AlertDialog dlg = new AlertDialog.Builder(mContext).create();
        dlg.show();
        Window window = dlg.getWindow();
        // *** 主要就是在这里实现这种效果的.
        window.setContentView(com.efounder.chat.R.layout.alertdialog);
        // 为确认按钮添加事件,执行退出应用操作
        TextView tv_paizhao = window.findViewById(com.efounder.chat.R.id.tv_content1);
        tv_paizhao.setText(ResStringUtil.getString(R.string.common_text_take_picture));
        tv_paizhao.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("SdCardPath")
            public void onClick(View v) {
                //pictureAndCropManager.takePicture();
                dlg.cancel();
                startTakePhoto();
            }
        });
        TextView tv_xiangce = window.findViewById(com.efounder.chat.R.id.tv_content2);
        tv_xiangce.setText(ResStringUtil.getString(R.string.common_text_photo_album));
        View lineView = window.findViewById(com.efounder.chat.R.id.line1);
        lineView.setVisibility(View.VISIBLE);
        tv_xiangce.setVisibility(View.VISIBLE);
        tv_xiangce.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                pictureAndCropManager.selectPhoto();
                dlg.cancel();
            }
        });

    }

    public void startTakePhoto() {
        if (EasyPermissionUtils.checkCameraPermission()) {
            pictureAndCropManager.takePicture();
        } else {

            EasyPermissionUtils.requestCameraPermission((Activity) mContext);
        }
    }

}
